//
//  ZQVisitroLoginView.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/13.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit
import SnapKit
//代理方法
@objc protocol ZQVisitroLoginViewDelegate : NSObjectProtocol {
    
 @objc optional func userWillLogin()
    
                 func userWillRegster()
    
    
}




//入口
class ZQVisitroLoginView: UIView {
    
    weak var delegate : ZQVisitroLoginViewDelegate?
    
    //外面用来设置页面的方法
    func vistitorInfo(title : String , ImageName : String? = nil) {
        //设置title
        tipLable.text = title
        
        if ImageName == nil {
            
            starAnimation()
            
        }else
        {
            circleView.image = UIImage(named:ImageName!)
            
            houseView.isHidden = true
            
            bringSubview(toFront: circleView)
        }
        
        
        
    }
    
    private func starAnimation () {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        //持续时间
        animation.duration = 20
        //次数
        animation.repeatCount = MAXFLOAT
        //自动移除动画
        animation.isRemovedOnCompletion = false
        //添加到视图
        self.circleView.layer.add(animation, forKey: nil)
        
    
    }
    
 
    //搭建UI界面
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    
    //布局View
    private func setupUI () {
        
        loginBtn.addTarget(self, action: #selector(clickLoginBtn), for:.touchUpInside)
        
        registerBtn.addTarget(self , action: #selector(clickRegisterBtn), for: .touchUpInside)
        
        //布局View
        
        addSubview(circleView)
        addSubview(backImageView)
        addSubview(houseView)
        addSubview(tipLable)
        //两个登陆按钮
        addSubview(loginBtn)
        addSubview(registerBtn)
       
        
        
    
        
        
        //约束
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        //约束房子
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //提示文字
        tipLable.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
        }
        //登陆
        loginBtn.snp.makeConstraints { (make) in
            make.trailing.equalTo(self.snp.centerX).offset(-7)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp.bottom).offset(20)
        }
        //注册
        registerBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(self.snp.centerX).offset(7)
            make.top.equalTo(tipLable.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        //遮挡物
        backImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(circleView)
        }
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        
        
    }
    
    //监听方法
    @objc private func clickLoginBtn () {
        
        delegate?.userWillLogin?()
        
    }
    
    @objc private func clickRegisterBtn () {
        
        delegate?.userWillRegster()
        
    }
    
    
    
    
    
   //懒加载界面控件
    //遮挡物
    private lazy var backImageView : UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //旋转背景
    
    private lazy var circleView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var houseView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //提示文字
    private lazy var tipLable : UILabel = {
        let leble = UILabel(title: "关注一些人,再回来看看有什么惊喜", fontSize: 14, textColor:UIColor.darkGray)
        leble.textAlignment = .center
        leble.numberOfLines = 0
        leble.preferredMaxLayoutWidth = 220
    
        return leble
    }()
    //登陆
    lazy var loginBtn : UIButton =  {
        
        let btn = UIButton(title: "登陆", fontSize: 14, titltColor:UIColor.darkGray)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return btn
    }()
    //注册
    lazy var registerBtn : UIButton = {
        
        let btn = UIButton(title: "注册", fontSize: 14, titltColor: UIColor.orange)
        
//        btn .setImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
       return btn
    }()
    
    
    
    
    
    

    }

    
